﻿<?php


if(isset($_SESSION['connexion'])){
	$conn = unserialize($_SESSION['connexion']);
	$q = new ClientQuery();
	$client = $q->findOneByEmail($conn->login);

	$addr = AdresseQuery::create()->findPk($client->getAdresse());

	if($client->getFacturation() == NULL){
		$addrDelivery = $addr;
	} else {
		$addrDelivery = AdresseQuery::create()->findPk($client->getFacturation());
	}

	$title = $addr->getTitre() == 0 ? "Mr" : "Mme";
	$name = $addr->getNom();
	$firstname = $addr->getPrenom();
	$address = $addr->getAdresse();
	$postalCode = $addr->getPostal();
	$city = $addr->getVille();
	$phone = $addr->getTel();
	$mail = $conn->login;

	$titleDelivery = $addrDelivery->getTitre() == 0 ? "Mr" : "Mme";
	$nameDelivery = $addrDelivery->getNom();
	$firstnameDelivery = $addrDelivery->getPrenom();
	$addressDelivery = $addrDelivery->getAdresse();
	$postalCodeDelivery = $addrDelivery->getPostal();
	$cityDelivery = $addrDelivery->getVille();
	$phoneDelivery = $addrDelivery->getTel();


	$smarty->assign("customer", array("title" => $title, "name" => $name, "firstname" => $firstname, "address" => $address, "postalCode" => $postalCode, "city" => $city, "phone" => $phone, "mail" => $mail));
	$smarty->assign("customerBis", array("titleDelivery" => $titleDelivery, "nameDelivery" => $nameDelivery, "firstnameDelivery" => $firstnameDelivery, "addressDelivery" => $addressDelivery, "postalCodeDelivery" => $postalCodeDelivery, "cityDelivery" => $cityDelivery, "phoneDelivery" => $phoneDelivery));
	
	$orders = array();
	$conn = unserialize($_SESSION['connexion']);
	$q = new ClientQuery();
	$client = $q->findOneByEmail($conn->login);
	
	$queryPanier = PanierQuery::create()->find();

	foreach($queryPanier as $p)
	{
		$total = 0;
		$fraisPort = 0;
		if($p->getClientId() == $client->getId()){
			$idOrder = $p->getId();
			$shirts = array();
			// panier correspondant
			$queryArticle = ArticleQuery::create()->findByPanierId($p->getId());
			foreach($queryArticle as $a)
			{
				$s = ShirtQuery::create()->findPk($a->getShirtId());
				array_push($shirts, array("id" => $s->getId(), "brand" => $s->getMarque(), "desc" => $s->getDescription(), "price" => $s->getPrix(), "ref" => $s->getId(), 
				"size" => $a->getTaille(), "color" => $a->getCouleur(), "quantity" => $a->getQuantite(), "sexe" => ($a->getSexe()==1)?'H':'F', "total" => ($s->getPrix() * $a->getQuantite())));
				$total += $a->getQuantite() * $s->getPrix();
			}
			
			$fraisPort = ($total<100)?10:0;
			
			array_push($orders, array("id" => $idOrder,
					"shirts" => $shirts,
					"total" => $total+$fraisPort,
					"fraisPort" => $fraisPort));
			
		}
		
	}
	
	$smarty->assign("orders", $orders);
	$smarty->display('account.tpl');
}
